/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.util.List;

import Util.HibernetUtil;
import model.Role;
import model.Site;
import model.User;
import model.alexa.Analyse;
import model.alexa.AudienceGeographique_M;
import model.alexa.Keyword_M;
import model.alexa.UpstreamSites_M;
import model.builtwith.AnalyseBuiltwith;
import model.builtwith.Categorie;
import model.builtwith.Thechnologie;
import model.header.AnalyseHeader;
import model.header.Header;
import model.performance.AnalysePerformance;
import model.seo.AnalyseSeo;

import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

import com.moteur.analysersite.alexa.UpstreamSites;

/**
 * 
 * @author mehdi
 */
public class Dao implements Idao {

	public void addUser(User user) {
		Session session = HibernetUtil.getSessionFactory().openSession();
		session.beginTransaction();
		session.save(user);
		session.getTransaction().commit();

	}

	public List<User> findAllUser() {
		Session session = HibernetUtil.getSessionFactory().openSession();
		session.beginTransaction();
		List<User> q = session.createQuery("from User").list();
		session.beginTransaction().commit();

		return q;

	}

	public User findUserByLogin(String login) {
		Session session = HibernetUtil.getSessionFactory().openSession();
		session.beginTransaction();
		List<User> q = session.createCriteria(User.class)
				.add(Restrictions.like("login", login)).list();

		User user = new User();
		for (User ac : q) {

			user = ac;
		}

		session.beginTransaction().commit();

		return user;

	}

	public User findUserById(Long id) {
		Session session = HibernetUtil.getSessionFactory().openSession();
		session.beginTransaction();
		User q = (User) session.get(User.class, id);

		User user = q;

		return user;

	}

	public void updateUser(User user) {
		Session session = HibernetUtil.getSessionFactory().openSession();
		session.beginTransaction();
		session.update(user);
		session.getTransaction().commit();

	}

	public void deleteUser(Long pk) {
		Session session = HibernetUtil.getSessionFactory().openSession();
		session.beginTransaction();
		User et = (User) session.load(User.class, pk);
		session.delete(et);
		session.beginTransaction().commit();
		session.close();

	}

	public List<Role> findALLRoll() {
		Session session = HibernetUtil.getSessionFactory().openSession();
		session.beginTransaction();
		List<Role> q = session.createQuery("from Role").list();
		session.beginTransaction().commit();

		return q;

	}

	public Role findRollbyId(Long pk) {
		Session session = HibernetUtil.getSessionFactory().openSession();
		session.beginTransaction();
		Role q = (Role) session.get(Role.class, pk);

		Role role = q;

		return role;
	}

	public void addSite(Site site) {
		// TODO Auto-generated method stub
		Session session = HibernetUtil.getSessionFactory().openSession();
		session.beginTransaction();
		session.save(site);
		session.getTransaction().commit();

	}

	public void updateSite(Site site) {
		Session session = HibernetUtil.getSessionFactory().openSession();
		session.beginTransaction();
		session.update(site);
		session.getTransaction().commit();

	}

	public List<Site> findAllSite() {
		Session session = HibernetUtil.getSessionFactory().openSession();
		session.beginTransaction();
		List<Site> q = session.createQuery("from Site").list();
		session.beginTransaction().commit();

		return q;

	}

	public Site findSitebyId(Long pk) {
		Session session = HibernetUtil.getSessionFactory().openSession();
		session.beginTransaction();
		Site q = (Site) session.get(Site.class, pk);

		Site site = q;

		return site;

	}

	public void deleteSite(Long pk) {
		Session session = HibernetUtil.getSessionFactory().openSession();
		session.beginTransaction();
		Site et = (Site) session.load(Site.class, pk);
		session.delete(et);
		session.beginTransaction().commit();
		session.close();

	}

	public void ajouterAnalyse(Analyse analyse) {

//		Session session = HibernetUtil.getSessionFactory().openSession();
//		session.beginTransaction();
//		session.save(analyse);
//		session.getTransaction().commit();

	}

	public void ajouterAudiance(AudienceGeographique_M audiance) {
		// TODO Auto-generated method stub

		Session session = HibernetUtil.getSessionFactory().openSession();
		session.beginTransaction();
		session.save(audiance);
System.out.println("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
		session.getTransaction().commit();
		System.out.println("bbbbbbbbbbbbbbbbbbbbbbbb");

	}

	public void ajouterUpstremSite(UpstreamSites_M upstream) {
		// TODO Auto-generated method stub

		Session session = HibernetUtil.getSessionFactory().openSession();
		session.beginTransaction();
		session.save(upstream);

		session.getTransaction().commit();

	}

	public void ajouteranalysebuilth(AnalyseBuiltwith builth) {
		// TODO Auto-generated method stub

		Session session = HibernetUtil.getSessionFactory().openSession();
		session.beginTransaction();
		session.save(builth);

		session.getTransaction().commit();

	}

	public void ajouterCategorie(Categorie cat, Session session) {
		session.beginTransaction();
		session.save(cat);

		session.getTransaction().commit();

	}

	public void ajoutertechnologie(Thechnologie tech, Session session) {
		// TODO Auto-generated method stub
		session.beginTransaction();
		session.save(tech);

		session.getTransaction().commit();

	}

	public void ajouteranalyseheader(AnalyseHeader analyse) {

		Session session = HibernetUtil.getSessionFactory().openSession();
		session.beginTransaction();
		session.save(analyse);

		session.getTransaction().commit();

	}

	public void ajouterHeader(Header head, Session session) {
		// TODO Auto-generated method stub
		session.beginTransaction();
		session.save(head);

		session.getTransaction().commit();

	}

	public void ajouteranalysePerformence(AnalysePerformance analyse) {
		Session session = HibernetUtil.getSessionFactory().openSession();
		session.beginTransaction();
		session.save(analyse);

		session.getTransaction().commit();

	}

	public void ajouterKeyword(Keyword_M keyword) {
		Session session = HibernetUtil.getSessionFactory().openSession();
		session.beginTransaction();
		session.save(keyword);

		session.getTransaction().commit();
		System.out.println("kewordaaa");

		
	}

	public void ajouterReferencement(AnalyseSeo analyse) {

		Session session = HibernetUtil.getSessionFactory().openSession();
		session.beginTransaction();
		session.save(analyse);

		session.getTransaction().commit();

		
	}

	public void deleteUser(String login) {
		Session session = HibernetUtil.getSessionFactory().openSession();
		session.beginTransaction();
		User et = (User) session.load(User.class, login);
		session.delete(et);
		session.beginTransaction().commit();
		session.close();

		
	}

}
